#!/use/bin/python
# coding=utf8
# Embedded file name: 19-find_service.py

import sys
import socket
import struct
import random
import time
import select

_https = False
try:
    import ssl

    _https = True
except:
    pass


def sockHttp(host, port, timeout=5):
    socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if port == 443:
        if not _https:
            raise "Not support SSL"
        try:
            socketObj = ssl.wrap_socket(socketObj, ssl_version=ssl.PROTOCOL_TLSv1)
        except ssl.SSLError as error:
            socketObj = ssl.wrap_socket(socketObj, ssl_version=ssl.PROTOL_SSLv23)

    socketObj.settimeout(timeout)
    socketObj.connect((host, port))
    return socketObj


class mssqlServerObj(object):
    def __init__(self):
        self.packetno = 0
        self.length = 0
        self.size = 0
        self.cli_version = 7
        self.cli_pid = 0
        self.conn_id = 0
        self.options_1 = 160
        self.options_2 = 3
        self.sqltype_flag = 0
        self.reserved_flag = 0
        self.time_zone = 0
        self.collation = 0
        self.version = 1895825409
        self.client = "Nmap"
        self.username = None
        self.password = None
        self.app = "Nmap NSE"
        self.server = "DUMMY"
        self.library = "mssql.lua"
        self.locale = ""
        self.database = "tempdb"
        self.MAC = "\x00\x00\x00\x00\x00\x00"

    def widechar(self, ch):
        return ch + "."

    def widestring(self, s):
        return "".join(map(self.widechar, s))

    def encryptpass(self, s):
        tmpLen = 23130
        passdata = ""
        for tmp in s:
            tmp = ord(tmp) ^ tmpLen
            passdata += struct.pack("H", tmp >> 4 & 3855 | tmp << 4 & 61680)

        return passdata

    def to_string(self):
        lenData = 86
        self.cli_pid = random.randint(1, 100000)

        self.length = lenData + 2 * (
            len(self.client) + len(self.username) + len(self.password) + len(self.app) + len(self.server) + len(
                self.library) + len(self.database))

        tcpData = struct.pack("<IIIIII", self.length, self.version, self.size, self.cli_version, self.cli_pid,
                              self.conn_id)
        tcpData += struct.pack("BBBB", self.options_1, self.options_2, self.sqltype_flag, self.reserved_flag)
        tcpData += struct.pack("<II", self.time_zone, self.collation)
        tcpData += struct.pack("<HH", lenData, len(self.client))
        lenData += len(self.client) * 2
        tcpData += struct.pack("<HH", lenData, len(self.username))
        lenData += len(self.username) * 2
        tcpData += struct.pack("<HH", lenData, len(self.password))
        lenData += len(self.password) * 2
        tcpData += struct.pack("<HH", lenData, len(self.app))
        lenData += len(self.app) * 2
        tcpData += struct.pack("<HH", lenData, len(self.server))
        lenData += len(self.server) * 2
        tcpData += struct.pack("<HH", 0, 0)
        tcpData += struct.pack("<HH", lenData, len(self.library))
        lenData += len(self.library) * 2
        tcpData += struct.pack("<HH", lenData, len(self.locale))
        lenData += len(self.locale) * 2
        tcpData += struct.pack("<HH", lenData, len(self.database))
        lenData += len(self.database) * 2
        tcpData += self.MAC
        tcpData += struct.pack("<H", lenData)
        tcpData += struct.pack("<H", 0)
        tcpData += struct.pack("<H", self.length)
        tcpData += struct.pack("<H", 0)
        tcpData += self.widestring(self.client)
        tcpData += self.widestring(self.username)
        tcpData += self.encryptpass(self.password)
        tcpData += self.widestring(self.app)
        tcpData += self.widestring(self.server)
        tcpData += self.widestring(self.library)
        tcpData += self.widestring(self.locale)
        tcpData += self.widestring(self.database)
        return tcpData

    def login(self, server, username, password, port, timeout):
        self.username = username
        self.password = password
        self.server = server
        data = self.to_string()
        datalen = len(data) + 8
        int_1 = 1
        int_0_1 = 0
        int_0 = 0
        self.packetno += 1
        int_16 = 16
        sockData = struct.pack(">BBHHBB%ds" % len(data), int_16, int_1, datalen, int_0_1, self.packetno,
                               int_0, data)
        socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        socketObj.settimeout(timeout)
        socketObj.connect((server, port))
        socketObj.send(sockData)
        read_N = ""
        Ii1I = 0
        int_1 = 0
        readOk = False
        Ooo = ""
        while True:
            if len(read_N) - Ii1I < 4:
                read = socketObj.recv(4)
                if len(read) > 0:
                    read_N += read
                    readOk = True
                else:
                    return None, "Fail to receive packet from MSSQL server"
            int_16, int_1, iI1 = struct.unpack_from(">BBH", read_N, Ii1I)
            Ii1I += 4
            if int_16 != 4:
                return None, "Server returned invalid packet"
            ii1I1i1I = iI1 - (len(read_N) - Ii1I + 4)
            if ii1I1i1I > 0:
                read = socketObj.recv(ii1I1i1I)
                if len(read) > 0:
                    read_N += read
                else:
                    return None, "Fail to receive packet from MSSQL server"
            # id, tag, version, count
            int_0_1, OOoo0O0, int_0, read = struct.unpack_from(">Hcc%ds" % (iI1 - 8), read_N, Ii1I)
            Ooo += read
            Ii1I += 4 + (iI1 - 8)
            if int_1 == 1:
                break

        socketObj.close()
        if not readOk:
            return None, "Unkown error"
        O0ooOooooO, = struct.unpack_from("B", Ooo, 0)
        if O0ooOooooO == 170:
            return False
        else:
            if O0ooOooooO == 227:
                return True
            return None, "Token ERROR"


def mssqlServer(arg):
    host, port = arg
    serverInfo = None
    try:
        mssql = mssqlServerObj()
        if not mssql.login(host, "sa", "thispassneverused$$$", port, 10):
            serverInfo = "mssql"
    except:
        pass

    return serverInfo


def SSHServer(arg):
    serverInfo = None
    serverHeader = None
    host, port = arg
    try:
        socketObj = sockHttp(host, port, 10)
        read = socketObj.recv(1024)
        serverHeader = read.strip()
        if read.find("SSH-") != -1:
            serverInfo = "ssh"
        elif read.find("220 ") == 0:
            if port == 25:
                serverInfo = "smtp"
            else:
                serverInfo = "ftp"
        elif read.find("MySQL") != -1 or read.find("mysqladmin") != -1:
            serverInfo = "mysql"
        elif read.find("RFB ") == 0:
            serverInfo = "vnc"
        elif len(read) > 5:
            read_regex = read.find(".", 5)
            if read_regex != -1:
                serverHeader = read[5:read_regex].decode("latin1")
                if len(serverHeader) > 2 and serverHeader[1] == ".":
                    serverInfo = "mysql"
        socketObj.close()
    except Exception as i1:
        pass

    if serverHeader:
        serverHeader = "".join(
            map(lambda x: (x if ord(x) >= 32 and ord(x) <= 126 else "\\x%02X" % ord(x)), serverHeader))
    return serverInfo, serverHeader


def rdpServer(arg):
    serverInfo = None
    host, port = arg
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send("\x03\x00\x00\x0b\x06\xe0\x00\x00\x00\x00\x00")
        if socketObj.recv(2) == "\x03\x00":
            serverInfo = "rdp"
        socketObj.close()
    except Exception as i1:
        pass

    return serverInfo


def httpServer(arg):
    serverInfo = None
    serverHeader = None
    host, port = arg
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send("GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % host)
        read = socketObj.recv(1024)
        if read.find("HTTP/1.") == 0 or read.lower().find("<html") > 0:
            if "access MongoDB" not in read:
                import re
                serverInfo = "www"
                if port == 443:
                    serverInfo = "ssl"
                read_regex = re.findall("^(Server|X\-[\w\-]*):\s+([^\r\n]+)", read, re.M | re.I)
                if read_regex:
                    serverHeader = str(read_regex)
        socketObj.close()
    except Exception as e:
        pass

    return serverInfo, serverHeader


def rsyncServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    header = {"MagicHeader": "@RSYNCD:",
              "HeaderVersion": " 30.0"}
    data = struct.pack("!8s5ss", header["MagicHeader"], header["HeaderVersion"], "\n")
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(data)
        read = socketObj.recv(14)
        if "RSYNCD" in read:
            serverInfo = "rsync"
            serverHeader = read.strip()
    except Exception as i1:
        pass

    return serverInfo, serverHeader


def memcachedServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    data = "version\n"
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(data)
        read = socketObj.recv(1024)
        if "VERSION" in read:
            serverInfo = "memcached"
            serverHeader = read.strip()
    except Exception as i1:
        pass

    return (serverInfo, serverHeader)


def smbServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    smbData = ("00000045ff534d42720000000000000800000000000000000000000" + \
               "0ffff000000000000002200024e54204c4d20302e31320002534d42" + \
               "20322e3030320002534d4220322e3f3f3f00".decode("hex"),)
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(smbData[0])
        read = socketObj.recv(1024)
        if read[0] == '''.''' and "SMB" in read:
            serverInfo = "smb"
    except Exception as i1:
        pass

    return serverInfo, serverHeader


def mongodbServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    mongodbHeader = "360000000100000000000000d40700000400000061646d696e" + \
                    "2e24636d640000000000ffffffff0f0000001070696e670001" + \
                    "00000000".decode("hex")
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(mongodbHeader)
        read = socketObj.recv(1024)

        if "\x11\x00\x00\x00\x01\x6f\x6b\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00" in read:
            serverInfo = "mongodb"

    except Exception as i1:
        pass

    return (serverInfo, serverHeader)


def jdwpServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    jdwpServerInfo = "JDWP-Handshake"
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(jdwpServerInfo)
        read = socketObj.recv(15)
        if "JDWP-Handshake" in read:
            serverInfo = "jdwp"
    except Exception as i1:
        pass
    return serverInfo, serverHeader


def postgresServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None

    postgresqlInfo = "\x00\x00\x00\x52\x00\x03\x00\x00\x75\x73\x65\x72\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x64\x61\x74\x61\x62\x61\x73\x65\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x61\x70\x70\x6c\x69\x63\x61\x74\x69\x6f\x6e\x5f\x6e\x61\x6d\x65\x00\x70\x67\x41\x64\x6d\x69\x6e\x20\x49\x49\x49\x20\x2d\x20\xe6\xb5\x8f\xe8\xa7\x88\xe5\x99\xa8\x00\x00"

    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(postgresqlInfo)
        read = socketObj.recv(15)
        if read.startswith("\x52\x00\x00\x00") or read.startswith("\x45\x00\x00\x00"):
            serverInfo = "postgresql"

    except Exception as i1:
        pass

    return serverInfo, serverHeader


def redisServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    tcpData = "*1\r\n$4\r\ninfo\r\n"
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(tcpData)
        read = socketObj.recv(200)
        if "redis_version" in read:
            serverInfo = "redis"
            ver = read.index("redis_version")
            serverHeader = read[ver + 14:ver + 20]
        elif "ERR operation not permitted" in read:
            serverInfo = "redis"
        elif "ERR wrong number" in read:
            serverInfo = "redis"
        elif "AUTH Authentication required" in read:
            serverInfo = "redis"
    except Exception as e:
        pass

    return serverInfo, serverHeader


def ldapServer(arg):
    host, port = arg
    serverInfo = None
    serverHeader = None
    tcpData = "\x30\x0c\x02\x01\x01\x60\x07\x02\x01\x02\x04\x00\x80\x00"
    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(tcpData)
        read = socketObj.recv(200)
        if read[4] == "":
            serverInfo = "ldap"
            if read[9] == '''.''':
                serverHeader = "ldapv2"
            elif read[9] == "":
                if "LDAPv3" in read[14:]:
                    serverHeader = "ldapv3"
                else:
                    serverHeader = "ldapv1"
    except Exception as i1:
        pass

    return serverInfo, serverHeader


def socks5Server(arg):
    host, port = arg
    serverInfo = None
    serverHeader = ""
    tcpData = "\x05\xff"
    for iii in range(255):
        tcpData += struct.pack("!B", iii)

    try:
        socketObj = sockHttp(host, port, 10)
        socketObj.send(tcpData)
        read = socketObj.recv(1024)
        if len(read) == 2 and read[0] == "":
            serverInfo = "socks5"
            reTcpData = struct.unpack("!B", read[1])[0]
            if reTcpData == 0:
                serverHeader = "NO AUTHENTICATION REQUIRED"
            elif reTcpData == 1:
                serverHeader = "GSSAPI"
            elif reTcpData == 2:
                serverHeader = "USERNAME/PASSWORD"
            elif reTcpData >= 3 and reTcpData <= 127:
                serverHeader = "IANA ASSIGNED"
            elif reTcpData >= 128 and reTcpData <= 254:
                serverHeader = "RESERVED FOR PRIVATE METHODS"
            elif reTcpData == "0xff":
                serverHeader = "NO ACCEPTABLE METHODS"
            else:
                serverHeader = ""
    except Exception as i1:
        pass

    return serverInfo, serverHeader


def port_list():
    return [1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31, 33, 35, 37, 38, 39, 41, 42,
            43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
            71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
            98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
            120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
            141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
            162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
            183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
            204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
            242, 243, 244, 245, 246, 247, 248, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 280,
            281, 282, 283, 284, 285, 286, 287, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321,
            322, 333, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362,
            363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383,
            384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404,
            405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425,
            426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446,
            447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467,
            468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488,
            489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509,
            510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530,
            531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551,
            552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572,
            573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593,
            594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614,
            615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635,
            636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656,
            657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677,
            678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698,
            699, 700, 702, 704, 705, 706, 707, 709, 710, 711, 721, 723, 729, 730, 731, 740, 741, 742, 744, 747, 748,
            749, 750, 751, 752, 753, 754, 758, 759, 760, 761, 762, 763, 764, 765, 767, 769, 770, 771, 772, 773, 774,
            775, 776, 777, 780, 781, 782, 783, 786, 787, 799, 800, 801, 808, 810, 828, 829, 847, 848, 860, 871, 873,
            886, 887, 888, 898, 900, 901, 902, 903, 904, 911, 912, 913, 927, 950, 953, 975, 989, 990, 991, 992, 993,
            994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1005, 1008, 1010, 1023, 1024, 1025, 1026, 1027, 1029, 1030,
            1031, 1032, 1033, 1034, 1035, 1036, 1040, 1042, 1045, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
            1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073,
            1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091,
            1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109,
            1110, 1111, 1112, 1114, 1115, 1116, 1117, 1119, 1120, 1122, 1123, 1124, 1125, 1126, 1127, 1139, 1154, 1155,
            1161, 1162, 1168, 1169, 1170, 1178, 1180, 1181, 1183, 1184, 1185, 1186, 1187, 1188, 1192, 1194, 1199, 1200,
            1201, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 1217, 1218,
            1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 1228, 1229, 1230, 1231, 1233, 1234, 1235, 1236, 1237,
            1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255,
            1256, 1257, 1258, 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 1272, 1273,
            1274, 1275, 1276, 1277, 1278, 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1288, 1289, 1290, 1291, 1292,
            1293, 1294, 1295, 1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310,
            1311, 1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 1327, 1328,
            1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343, 1344, 1345, 1346,
            1347, 1348, 1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1360, 1361, 1362, 1363, 1364,
            1365, 1366, 1367, 1368, 1369, 1370, 1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1380, 1381, 1382,
            1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391, 1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400,
            1401, 1402, 1403, 1404, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1412, 1413, 1414, 1415, 1416, 1417, 1418,
            1419, 1420, 1421, 1422, 1423, 1424, 1425, 1426, 1427, 1428, 1429, 1430, 1431, 1432, 1433, 1434, 1435, 1436,
            1437, 1438, 1439, 1440, 1441, 1442, 1443, 1444, 1445, 1446, 1447, 1448, 1449, 1450, 1451, 1452, 1453, 1454,
            1455, 1456, 1457, 1458, 1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1468, 1469, 1470, 1471, 1472,
            1473, 1474, 1475, 1476, 1477, 1478, 1479, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1489, 1490,
            1491, 1492, 1493, 1494, 1495, 1496, 1497, 1498, 1499, 1500, 1501, 1502, 1503, 1504, 1505, 1506, 1507, 1508,
            1509, 1510, 1511, 1512, 1513, 1514, 1515, 1516, 1517, 1518, 1519, 1520, 1521, 1522, 1523, 1524, 1525, 1526,
            1527, 1528, 1529, 1530, 1531, 1532, 1533, 1534, 1535, 1536, 1537, 1538, 1539, 1540, 1541, 1542, 1543, 1544,
            1545, 1546, 1547, 1548, 1549, 1550, 1551, 1552, 1553, 1554, 1555, 1556, 1557, 1558, 1559, 1560, 1561, 1562,
            1563, 1564, 1565, 1566, 1567, 1568, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1580,
            1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1593, 1594, 1595, 1596, 1597, 1598,
            1599, 1600, 1601, 1602, 1603, 1604, 1605, 1606, 1607, 1608, 1609, 1610, 1611, 1612, 1613, 1614, 1615, 1616,
            1617, 1618, 1619, 1620, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629, 1630, 1631, 1632, 1633, 1634,
            1635, 1636, 1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652,
            1653, 1654, 1655, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668, 1669, 1670,
            1671, 1672, 1673, 1674, 1675, 1676, 1677, 1678, 1679, 1680, 1681, 1682, 1683, 1684, 1685, 1686, 1687, 1688,
            1689, 1690, 1691, 1692, 1693, 1694, 1695, 1696, 1697, 1698, 1699, 1700, 1701, 1702, 1703, 1704, 1705, 1706,
            1707, 1708, 1709, 1710, 1711, 1712, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721, 1722, 1723, 1724,
            1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734, 1735, 1736, 1737, 1738, 1739, 1740, 1741, 1742,
            1743, 1744, 1745, 1746, 1747, 1748, 1749, 1750, 1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760,
            1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1776, 1777, 1778, 1779,
            1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797,
            1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813, 1814, 1815,
            1816, 1817, 1818, 1819, 1820, 1821, 1822, 1823, 1824, 1825, 1826, 1827, 1828, 1829, 1830, 1831, 1832, 1833,
            1834, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849, 1850, 1851,
            1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869,
            1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887,
            1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905,
            1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923,
            1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941,
            1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959,
            1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977,
            1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
            1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
            2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2030, 2032, 2033,
            2034, 2035, 2037, 2038, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053,
            2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2067, 2068, 2069, 2070, 2071, 2072,
            2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083, 2086, 2087, 2089, 2090, 2091, 2092, 2093,
            2094, 2095, 2096, 2097, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105, 2106, 2107, 2108, 2109, 2110, 2111,
            2112, 2113, 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2123, 2124, 2125, 2126, 2127, 2128, 2129,
            2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147,
            2148, 2149, 2150, 2151, 2152, 2155, 2159, 2160, 2161, 2162, 2163, 2164, 2165, 2166, 2167, 2170, 2171, 2172,
            2173, 2174, 2175, 2176, 2177, 2180, 2181, 2190, 2191, 2199, 2200, 2201, 2202, 2207, 2213, 2220, 2221, 2222,
            2223, 2224, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2239, 2240, 2241, 2242, 2243, 2244, 2245, 2246, 2248,
            2249, 2250, 2251, 2252, 2253, 2254, 2255, 2260, 2273, 2279, 2280, 2281, 2282, 2283, 2284, 2285, 2286, 2287,
            2288, 2289, 2294, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302, 2303, 2304, 2305, 2306, 2307, 2308, 2309,
            2310, 2311, 2313, 2314, 2315, 2316, 2317, 2318, 2319, 2320, 2321, 2322, 2323, 2324, 2325, 2326, 2327, 2328,
            2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346,
            2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358, 2359, 2360, 2361, 2362, 2363, 2364,
            2365, 2366, 2367, 2368, 2369, 2370, 2371, 2381, 2382, 2383, 2384, 2385, 2386, 2387, 2388, 2389, 2390, 2391,
            2392, 2393, 2394, 2395, 2396, 2397, 2398, 2399, 2400, 2401, 2402, 2403, 2404, 2405, 2406, 2407, 2408, 2409,
            2410, 2411, 2412, 2413, 2414, 2415, 2416, 2417, 2418, 2419, 2420, 2421, 2422, 2423, 2424, 2425, 2427, 2428,
            2429, 2430, 2431, 2432, 2433, 2434, 2435, 2436, 2437, 2438, 2439, 2440, 2441, 2442, 2443, 2444, 2445, 2446,
            2447, 2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2456, 2457, 2458, 2459, 2460, 2461, 2462, 2463, 2464,
            2465, 2466, 2467, 2468, 2469, 2470, 2471, 2472, 2473, 2474, 2475, 2476, 2477, 2478, 2479, 2480, 2481, 2482,
            2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491, 2492, 2493, 2494, 2495, 2496, 2497, 2498, 2499, 2500,
            2501, 2502, 2503, 2504, 2505, 2506, 2507, 2508, 2509, 2510, 2511, 2512, 2513, 2514, 2515, 2516, 2517, 2518,
            2519, 2520, 2521, 2522, 2523, 2524, 2525, 2526, 2527, 2528, 2529, 2530, 2531, 2532, 2533, 2534, 2535, 2536,
            2537, 2538, 2539, 2540, 2541, 2542, 2543, 2544, 2545, 2546, 2547, 2548, 2549, 2550, 2551, 2552, 2553, 2554,
            2555, 2556, 2557, 2558, 2559, 2560, 2561, 2562, 2563, 2564, 2565, 2566, 2567, 2568, 2569, 2570, 2571, 2572,
            2573, 2574, 2575, 2576, 2577, 2578, 2579, 2580, 2581, 2582, 2583, 2584, 2585, 2586, 2587, 2588, 2589, 2590,
            2591, 2592, 2593, 2594, 2595, 2596, 2597, 2598, 2599, 2600, 2601, 2602, 2603, 2604, 2605, 2606, 2607, 2608,
            2609, 2610, 2611, 2612, 2613, 2614, 2615, 2616, 2617, 2618, 2619, 2620, 2621, 2622, 2623, 2624, 2625, 2626,
            2627, 2628, 2629, 2630, 2631, 2632, 2633, 2634, 2635, 2636, 2637, 2638, 2639, 2640, 2641, 2642, 2643, 2644,
            2645, 2646, 2647, 2648, 2649, 2650, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 2658, 2659, 2660, 2661, 2662,
            2663, 2664, 2665, 2666, 2667, 2668, 2669, 2670, 2671, 2672, 2673, 2674, 2675, 2676, 2677, 2678, 2679, 2680,
            2681, 2683, 2684, 2685, 2686, 2687, 2688, 2689, 2690, 2691, 2692, 2693, 2694, 2695, 2696, 2697, 2698, 2699,
            2700, 2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2711, 2712, 2713, 2714, 2715, 2716, 2717,
            2718, 2719, 2720, 2721, 2722, 2723, 2724, 2725, 2726, 2727, 2728, 2729, 2730, 2731, 2732, 2733, 2734, 2735,
            2736, 2737, 2738, 2739, 2740, 2741, 2742, 2743, 2744, 2745, 2746, 2747, 2748, 2749, 2750, 2751, 2752, 2753,
            2754, 2755, 2756, 2757, 2758, 2759, 2760, 2761, 2762, 2763, 2764, 2765, 2766, 2767, 2768, 2769, 2770, 2771,
            2772, 2773, 2774, 2775, 2776, 2777, 2778, 2779, 2780, 2781, 2782, 2783, 2784, 2785, 2786, 2787, 2788, 2789,
            2790, 2791, 2792, 2793, 2794, 2795, 2796, 2797, 2798, 2799, 2800, 2801, 2802, 2803, 2804, 2805, 2806, 2807,
            2808, 2809, 2810, 2811, 2812, 2813, 2814, 2815, 2816, 2817, 2818, 2819, 2820, 2821, 2822, 2823, 2824, 2826,
            2827, 2828, 2829, 2830, 2831, 2832, 2833, 2834, 2835, 2836, 2837, 2838, 2839, 2840, 2841, 2842, 2843, 2844,
            2845, 2846, 2847, 2848, 2849, 2850, 2851, 2852, 2853, 2854, 2856, 2857, 2858, 2859, 2860, 2861, 2862, 2863,
            2864, 2865, 2866, 2867, 2868, 2869, 2870, 2871, 2872, 2873, 2874, 2875, 2876, 2877, 2878, 2879, 2880, 2881,
            2882, 2883, 2884, 2885, 2886, 2887, 2888, 2889, 2890, 2891, 2892, 2893, 2894, 2895, 2896, 2897, 2898, 2899,
            2900, 2901, 2902, 2903, 2904, 2905, 2906, 2907, 2908, 2909, 2910, 2911, 2912, 2913, 2914, 2915, 2916, 2917,
            2918, 2919, 2920, 2921, 2922, 2923, 2924, 2926, 2927, 2928, 2929, 2930, 2931, 2932, 2933, 2934, 2935, 2936,
            2937, 2938, 2939, 2940, 2941, 2942, 2943, 2944, 2945, 2946, 2947, 2948, 2949, 2950, 2951, 2952, 2953, 2954,
            2955, 2956, 2957, 2958, 2959, 2960, 2961, 2962, 2963, 2964, 2965, 2966, 2967, 2968, 2969, 2970, 2971, 2972,
            2973, 2974, 2975, 2976, 2977, 2978, 2979, 2980, 2981, 2982, 2983, 2984, 2985, 2986, 2987, 2988, 2989, 2990,
            2991, 2992, 2993, 2994, 2995, 2996, 2997, 2998, 2999, 3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008,
            3009, 3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3023, 3024, 3025, 3026,
            3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3035, 3036, 3037, 3038, 3039, 3040, 3041, 3042, 3043, 3044,
            3045, 3046, 3047, 3048, 3049, 3050, 3051, 3052, 3053, 3054, 3055, 3056, 3057, 3058, 3059, 3060, 3061, 3062,
            3063, 3064, 3065, 3066, 3067, 3068, 3069, 3070, 3071, 3072, 3073, 3074, 3075, 3076, 3077, 3078, 3079, 3080,
            3081, 3082, 3083, 3084, 3085, 3086, 3087, 3088, 3089, 3090, 3091, 3092, 3093, 3094, 3095, 3096, 3098, 3099,
            3100, 3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108, 3109, 3110, 3111, 3112, 3113, 3114, 3115, 3116, 3117,
            3118, 3119, 3120, 3121, 3122, 3123, 3124, 3125, 3126, 3127, 3128, 3129, 3130, 3131, 3132, 3133, 3134, 3135,
            3136, 3137, 3138, 3139, 3140, 3141, 3142, 3143, 3144, 3145, 3146, 3147, 3148, 3149, 3150, 3151, 3152, 3153,
            3154, 3155, 3156, 3157, 3158, 3159, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, 3171,
            3172, 3173, 3174, 3175, 3176, 3177, 3178, 3179, 3180, 3181, 3182, 3183, 3184, 3185, 3186, 3187, 3188, 3189,
            3190, 3191, 3192, 3193, 3194, 3195, 3196, 3197, 3198, 3199, 3200, 3201, 3202, 3203, 3204, 3205, 3206, 3207,
            3208, 3209, 3210, 3211, 3212, 3213, 3214, 3215, 3216, 3217, 3218, 3219, 3220, 3221, 3222, 3223, 3224, 3225,
            3226, 3227, 3228, 3229, 3230, 3231, 3232, 3233, 3234, 3235, 3236, 3237, 3238, 3239, 3240, 3241, 3242, 3243,
            3244, 3245, 3246, 3247, 3248, 3249, 3250, 3251, 3252, 3253, 3254, 3255, 3256, 3257, 3258, 3259, 3260, 3261,
            3262, 3263, 3264, 3265, 3266, 3267, 3268, 3269, 3270, 3271, 3272, 3273, 3274, 3275, 3276, 3277, 3278, 3279,
            3280, 3281, 3282, 3283, 3284, 3285, 3286, 3287, 3288, 3289, 3290, 3291, 3292, 3293, 3294, 3295, 3296, 3297,
            3298, 3299, 3302, 3303, 3304, 3305, 3306, 3307, 3308, 3309, 3310, 3311, 3312, 3313, 3314, 3315, 3316, 3317,
            3318, 3319, 3320, 3321, 3326, 3327, 3328, 3329, 3330, 3331, 3332, 3333, 3334, 3335, 3336, 3337, 3338, 3339,
            3340, 3341, 3342, 3343, 3344, 3345, 3346, 3347, 3348, 3349, 3350, 3351, 3352, 3353, 3354, 3355, 3356, 3357,
            3358, 3359, 3360, 3361, 3362, 3363, 3364, 3365, 3366, 3372, 3373, 3374, 3375, 3376, 3377, 3378, 3379, 3380,
            3381, 3382, 3383, 3384, 3385, 3386, 3387, 3388, 3389, 3390, 3391, 3392, 3393, 3394, 3395, 3396, 3397, 3398,
            3399, 3400, 3401, 3402, 3403, 3405, 3406, 3407, 3408, 3409, 3410, 3411, 3412, 3413, 3414, 3415, 3416, 3417,
            3418, 3419, 3420, 3421, 3422, 3423, 3424, 3425, 3426, 3427, 3428, 3429, 3430, 3431, 3432, 3433, 3434, 3435,
            3436, 3437, 3438, 3439, 3440, 3441, 3442, 3443, 3444, 3445, 3446, 3447, 3448, 3449, 3450, 3451, 3452, 3453,
            3454, 3455, 3456, 3457, 3458, 3459, 3460, 3461, 3462, 3463, 3464, 3465, 3466, 3467, 3468, 3469, 3470, 3471,
            3472, 3473, 3474, 3475, 3476, 3477, 3478, 3479, 3480, 3481, 3482, 3483, 3484, 3485, 3486, 3487, 3488, 3489,
            3490, 3491, 3492, 3493, 3494, 3495, 3496, 3497, 3498, 3499, 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507,
            3508, 3509, 3510, 3511, 3512, 3513, 3514, 3515, 3516, 3517, 3518, 3519, 3520, 3521, 3522, 3523, 3524, 3525,
            3526, 3527, 3528, 3529, 3530, 3531, 3532, 3533, 3534, 3535, 3536, 3537, 3538, 3539, 3540, 3541, 3542, 3543,
            3544, 3545, 3547, 3548, 3549, 3550, 3551, 3552, 3553, 3554, 3555, 3556, 3557, 3558, 3559, 3560, 3561, 3562,
            3563, 3564, 3565, 3566, 3567, 3568, 3569, 3570, 3571, 3572, 3573, 3574, 3575, 3576, 3577, 3578, 3579, 3580,
            3581, 3582, 3583, 3584, 3585, 3586, 3587, 3588, 3589, 3590, 3591, 3592, 3593, 3594, 3595, 3596, 3597, 3598,
            3599, 3600, 3601, 3602, 3603, 3604, 3605, 3606, 3607, 3608, 3609, 3610, 3611, 3612, 3613, 3614, 3615, 3616,
            3617, 3618, 3619, 3620, 3621, 3622, 3623, 3624, 3625, 3626, 3627, 3628, 3629, 3630, 3631, 3632, 3633, 3634,
            3635, 3636, 3637, 3638, 3639, 3640, 3641, 3642, 3643, 3644, 3645, 3646, 3647, 3648, 3649, 3650, 3651, 3652,
            3653, 3654, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662, 3663, 3664, 3665, 3666, 3667, 3668, 3669, 3670,
            3671, 3672, 3673, 3674, 3675, 3676, 3677, 3678, 3679, 3680, 3681, 3682, 3683, 3684, 3685, 3686, 3687, 3688,
            3689, 3690, 3691, 3692, 3693, 3694, 3695, 3696, 3697, 3698, 3699, 3700, 3701, 3702, 3703, 3704, 3705, 3706,
            3707, 3709, 3710, 3711, 3712, 3713, 3714, 3715, 3716, 3717, 3718, 3719, 3720, 3721, 3722, 3723, 3724, 3725,
            3726, 3727, 3728, 3729, 3730, 3731, 3732, 3733, 3734, 3735, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743,
            3744, 3745, 3746, 3747, 3748, 3749, 3750, 3751, 3752, 3753, 3754, 3755, 3756, 3757, 3758, 3759, 3760, 3761,
            3762, 3763, 3764, 3765, 3767, 3768, 3769, 3770, 3772, 3773, 3774, 3775, 3776, 3777, 3778, 3779, 3780, 3781,
            3782, 3783, 3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799,
            3800, 3802, 3845, 3846, 3847, 3848, 3849, 3850, 3851, 3852, 3853, 3854, 3855, 3856, 3857, 3858, 3859, 3860,
            3861, 3862, 3863, 3864, 3865, 3866, 3867, 3868, 3869, 3870, 3871, 3875, 3876, 3885, 3900, 3928, 3929, 3939,
            3959, 3970, 3971, 3984, 3985, 3986, 3987, 3994, 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008,
            4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019, 4020, 4021, 4022, 4023, 4024, 4025, 4026,
            4027, 4028, 4029, 4030, 4031, 4032, 4033, 4034, 4035, 4036, 4040, 4041, 4042, 4045, 4080, 4081, 4096, 4097,
            4098, 4099, 4100, 4105, 4111, 4114, 4132, 4133, 4134, 4138, 4141, 4142, 4143, 4144, 4145, 4154, 4160, 4199,
            4200, 4201, 4242, 4274, 4300, 4321, 4333, 4343, 4344, 4345, 4346, 4347, 4348, 4349, 4350, 4351, 4353, 4354,
            4355, 4356, 4357, 4358, 4369, 4400, 4442, 4443, 4444, 4445, 4446, 4447, 4448, 4449, 4450, 4451, 4452, 4453,
            4454, 4455, 4456, 4457, 4480, 4500, 4545, 4546, 4547, 4555, 4557, 4559, 4567, 4568, 4569, 4600, 4601, 4658,
            4659, 4660, 4661, 4662, 4672, 4711, 4752, 4800, 4801, 4802, 4827, 4837, 4838, 4839, 4848, 4849, 4868, 4869,
            4885, 4888, 4894, 4899, 4950, 4983, 4987, 4988, 4989, 4998, 5000, 5001, 5002, 5003, 5004, 5005, 5006, 5007,
            5008, 5009, 5010, 5011, 5020, 5021, 5022, 5023, 5024, 5025, 5031, 5042, 5050, 5051, 5052, 5053, 5054, 5055,
            5056, 5057, 5060, 5061, 5064, 5065, 5066, 5069, 5071, 5081, 5093, 5099, 5100, 5101, 5102, 5108, 5137, 5145,
            5150, 5151, 5152, 5154, 5165, 5168, 5177, 5178, 5190, 5191, 5192, 5193, 5200, 5201, 5202, 5203, 5222, 5223,
            5225, 5226, 5232, 5236, 5250, 5251, 5264, 5265, 5269, 5272, 5282, 5300, 5301, 5302, 5303, 5304, 5305, 5306,
            5307, 5308, 5309, 5310, 5311, 5314, 5315, 5347, 5351, 5352, 5353, 5354, 5355, 5400, 5401, 5402, 5403, 5404,
            5405, 5406, 5407, 5408, 5409, 5410, 5411, 5412, 5413, 5414, 5415, 5416, 5417, 5418, 5419, 5420, 5421, 5422,
            5423, 5424, 5425, 5426, 5427, 5428, 5429, 5430, 5431, 5432, 5435, 5444, 5454, 5455, 5456, 5461, 5462, 5463,
            5465, 5485, 5500, 5501, 5502, 5503, 5504, 5510, 5520, 5521, 5530, 5540, 5550, 5553, 5554, 5555, 5556, 5566,
            5569, 5595, 5596, 5597, 5598, 5599, 5600, 5601, 5602, 5603, 5604, 5605, 5631, 5632, 5666, 5673, 5674, 5675,
            5676, 5677, 5678, 5679, 5680, 5688, 5690, 5713, 5714, 5715, 5716, 5717, 5720, 5727, 5729, 5730, 5741, 5742,
            5745, 5746, 5755, 5757, 5766, 5767, 5768, 5771, 5800, 5801, 5802, 5803, 5813, 5858, 5859, 5882, 5888, 5889,
            5900, 5901, 5902, 5903, 5968, 5969, 5977, 5978, 5979, 5987, 5988, 5989, 5990, 5991, 5997, 5998, 5999, 6000,
            6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, 6050, 6051, 6064, 6065, 6066, 6067, 6068, 6069,
            6070, 6071, 6072, 6073, 6080, 6085, 6100, 6101, 6102, 6103, 6104, 6105, 6106, 6107, 6108, 6109, 6110, 6111,
            6112, 6123, 6129, 6141, 6142, 6143, 6144, 6145, 6146, 6147, 6148, 6149, 6150, 6175, 6176, 6177, 6200, 6253,
            6255, 6270, 6300, 6321, 6322, 6343, 6346, 6347, 6373, 6382, 6389, 6400, 6443, 6455, 6456, 6471, 6500, 6501,
            6502, 6503, 6505, 6506, 6507, 6508, 6509, 6510, 6543, 6547, 6548, 6549, 6550, 6558, 6566, 6580, 6581, 6582,
            6588, 6620, 6621, 6623, 6628, 6631, 6665, 6666, 6667, 6668, 6669, 6670, 6672, 6673, 6680, 6699, 6700, 6701,
            6714, 6723, 6767, 6768, 6776, 6777, 6778, 6788, 6789, 6790, 6831, 6841, 6842, 6850, 6881, 6882, 6883, 6884,
            6885, 6886, 6887, 6888, 6889, 6891, 6900, 6901, 6939, 6961, 6962, 6963, 6964, 6965, 6966, 6969, 6970, 6998,
            6999, 7000, 7001, 7002, 7003, 7004, 7005, 7006, 7007, 7008, 7009, 7010, 7011, 7012, 7013, 7014, 7015, 7020,
            7021, 7030, 7070, 7080, 7099, 7100, 7101, 7111, 7121, 7144, 7161, 7170, 7174, 7200, 7201, 7205, 7210, 7211,
            7269, 7273, 7280, 7281, 7283, 7300, 7320, 7323, 7326, 7391, 7392, 7395, 7426, 7427, 7428, 7429, 7430, 7431,
            7437, 7461, 7464, 7491, 7500, 7501, 7505, 7510, 7511, 7544, 7545, 7560, 7566, 7570, 7575, 7580, 7585, 7588,
            7597, 7624, 7626, 7627, 7633, 7634, 7648, 7649, 7666, 7674, 7675, 7676, 7743, 7775, 7776, 7777, 7778, 7779,
            7781, 7786, 7797, 7798, 7800, 7801, 7845, 7846, 7875, 7902, 7913, 7932, 7933, 7938, 7955, 7967, 7979, 7980,
            7999, 8000, 8001, 8002, 8003, 8004, 8005, 8007, 8008, 8009, 8010, 8019, 8022, 8028, 8030, 8032, 8033, 8044,
            8060, 8074, 8077, 8080, 8081, 8082, 8083, 8088, 8089, 8090, 8098, 8100, 8115, 8116, 8118, 8121, 8122, 8123,
            8129, 8130, 8131, 8132, 8160, 8161, 8181, 8182, 8183, 8184, 8185, 8186, 8187, 8188, 8189, 8190, 8191, 8192,
            8193, 8194, 8199, 8200, 8201, 8204, 8205, 8206, 8207, 8208, 8216, 8224, 8225, 8245, 8300, 8302, 8310, 8311,
            8351, 8376, 8377, 8378, 8379, 8380, 8383, 8390, 8400, 8401, 8402, 8403, 8416, 8417, 8431, 8443, 8444, 8450,
            8473, 8484, 8500, 8554, 8530, 8555, 8649, 8733, 8763, 8764, 8765, 8786, 8787, 8804, 8834, 8863, 8864, 8875,
            8866, 8880, 8888, 8887, 8889, 8890, 8891, 8892, 8893, 8894, 8900, 8901, 8910, 8911, 8954, 8989, 8987, 8999,
            9000, 9001, 9002, 9006, 9009, 9020, 9021, 9022, 9023, 9024, 9025, 9026, 9080, 9043, 9050, 9090, 9091, 9100,
            9099, 9101, 9102, 9103, 9110, 9111, 9131, 9152, 9160, 9161, 9162, 9163, 9164, 9200, 9201, 9202, 9203, 9204,
            9205, 9206, 9207, 9210, 9211, 9217, 9216, 9281, 9282, 9283, 9284, 9285, 9287, 9292, 9321, 9343, 9344, 9346,
            9374, 9396, 9397, 9400, 9418, 9495, 9433, 9443, 9500, 9535, 9536, 9537, 9593, 9594, 9595, 9600, 9612, 9704,
            9747, 9753, 9797, 9800, 9801, 9802, 9872, 9875, 9876, 9888, 9889, 9898, 9899, 9900, 9901, 9909, 9911, 9950,
            9951, 9952, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000, 10001, 10002, 10003, 10004,
            10005, 10007, 10008, 10012, 10051, 10080, 10081, 10082, 10083, 10101, 10102, 10103, 10113, 10114, 10115,
            10116, 10128, 10168, 10202, 10203, 10204, 10252, 10260, 10288, 10607, 10616, 10617, 10626, 10628, 10666,
            10752, 10990, 11000, 11001, 11111, 11201, 11211, 11223, 11319, 11320, 11321, 11367, 11371, 11600, 11720,
            11751, 11965, 11967, 11999, 12000, 12001, 12002, 12003, 12004, 12005, 12006, 12076, 12109, 12168, 12172,
            12174, 12223, 12321, 12345, 12346, 12361, 12362, 12468, 12701, 12753, 12754, 13160, 13223, 13224, 13666,
            13701, 13702, 13705, 13706, 13708, 13709, 13710, 13711, 13712, 13713, 13714, 13715, 13716, 13717, 13718,
            13720, 13721, 13722, 13724, 13782, 13783, 13818, 13819, 13820, 13821, 13822, 14001, 14002, 14033, 14034,
            14141, 14145, 14149, 14194, 14237, 14936, 14937, 14942, 15000, 15104, 15126, 15345, 15363, 15858, 16360,
            16361, 16367, 16368, 16384, 16660, 16661, 16959, 16969, 16991, 17007, 17185, 17219, 17300, 17770, 17771,
            17772, 17990, 18000, 18181, 18182, 18183, 18184, 18185, 18186, 18187, 18190, 18241, 18264, 18463, 18769,
            18888, 19191, 19194, 19226, 19283, 19315, 19398, 19410, 19411, 19412, 19540, 19541, 19638, 19726, 20000,
            20001, 20005, 20011, 20012, 20020, 20031, 20034, 20168, 20200, 20202, 20203, 20221, 20222, 20670, 20999,
            21000, 21064, 21065, 21071, 21227, 21317, 21490, 21544, 21554, 21590, 21700, 21800, 21845, 21846, 21847,
            21848, 21849, 22000, 22001, 22002, 22003, 22222, 22273, 22289, 22305, 22321, 22370, 22555, 22800, 22951,
            23456, 23945, 24000, 24001, 24002, 24003, 24004, 24005, 24006, 24242, 24249, 24345, 24346, 24347, 24386,
            24554, 24677, 24678, 24922, 25000, 25001, 25002, 25003, 25004, 25005, 25006, 25007, 25008, 25009, 25378,
            25544, 25793, 25867, 25901, 25903, 26000, 26208, 26260, 26261, 26262, 26263, 26264, 27000, 27001, 27002,
            27003, 27004, 27005, 27006, 27007, 27008, 27009, 27010, 27017, 27345, 27374, 27504, 27665, 27960, 27999,
            28001, 28017, 29559, 29891, 30001, 30002, 30100, 30101, 30102, 30303, 30999, 31337, 31339, 31416, 31457,
            31554, 31556, 31620, 31765, 31785, 31786, 31787, 32000, 32123, 32261, 32666, 32768, 32769, 32770, 32771,
            32772, 32773, 32774, 32775, 32776, 32777, 32778, 32779, 32780, 32786, 32787, 32896, 33270, 33331, 33434,
            33567, 33568, 33911, 34012, 34249, 34324, 34567, 34952, 36079, 36794, 36865, 37475, 37651, 38037, 38201,
            38292, 38293, 39681, 40001, 40412, 40841, 40842, 40843, 41002, 41080, 41111, 41443, 41508, 41794, 41795,
            42508, 42509, 42510, 42800, 43118, 43188, 43189, 43190, 44321, 44322, 44333, 44334, 44337, 44442, 44443,
            44818, 45000, 45054, 45678, 45966, 47000, 47557, 47624, 47806, 47808, 47891, 48000, 48001, 48002, 48003,
            48556, 49400, 50000, 50001, 50002, 50003, 50004, 50013, 50123, 50505, 50776, 51051, 51210, 53001, 54320,
            54321, 54345, 57341, 59595, 60008, 60177, 60179, 61439, 61440, 61441, 61446, 65000, 65301, 65534]


def sockSend(host, port, timeout):
    try:
        socketObj = sockHttp(host, port, timeout)
        try:
            socketObj.settimeout(0.5)
            socketObj.recv(0)
        except socket.timeout:
            pass

        socketObj.close()
        return True
    except Exception as i1:
        pass

    return False


def scanPort(host, ports, timeout):
    portList = []
    sockList = set()
    while len(ports):
        socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        socketObj.setblocking(0)
        socketObj.connect_ex((host, ports.pop()))
        sockList.add(socketObj)
        if len(sockList) < 200 and len(ports) > 0:
            continue
        start_time = time.time()
        try:
            while True:
                # readable,writeable,exceptional
                readable, writeable, exceptional = select.select([], sockList, [], 0.5)
                if len(writeable) > 0:
                    for socketObj in writeable:
                        strtus = socketObj.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
                        if strtus == 0:
                            host, port = socketObj.getpeername()
                            portList.append(port)
                        socketObj.close()

                    sockList = sockList - set(writeable)
                if time.time() - start_time > timeout:
                    break
        except:
            pass

        sockList = set()

    portList.sort()
    return portList


def assign(service, arg):
    if service != "ip":
        return
    else:
        return (True, arg)


def audit(arg):
    host = arg
    checkServer = [([80, 8080, 81, 443], httpServer),
                   ([22, 21, 25, 3306, 5900], SSHServer),
                   ([3389], rdpServer),
                   ([1433], mssqlServer),
                   ([873], rsyncServer),
                   ([11211], memcachedServer),
                   ([445], smbServer),
                   ([27017], mongodbServer),
                   ([3999, 5000, 5005, 8000, 8453, 8787, 8788, 8789, 9001, 9871, 18000], jdwpServer),
                   ([6379], redisServer),
                   ([5432], postgresServer),
                   ([1080], socks5Server),
                   ([389, 636], ldapServer)]
    ports = []
    linkPorts = []
    for port, getSer in checkServer:
        linkPorts += port

    ports += linkPorts

    if _G["scanport"]:
        if not (sockSend(host, 190, 5) or sockSend(host, 86, 5)):
            ports += port_list()
            ports = set(ports)

    linkPort = []
    linkPort = scanPort(arg, ports, 10)
    if linkPort:
        if len(linkPort) < 50:
            security_note("TCP: " + repr(linkPort))
        else:
            linkPort = linkPorts
    if 80 not in linkPort:
        linkPort.append(80)
    targetList = []
    for port, serverScanObj in checkServer:
        port = list(set(port + linkPort))
        port.sort()
        targetList.append((port, serverScanObj))

    scanResult = []
    okPort = []
    for linkPort, serverScanObj in targetList:
        for port in linkPort:
            if port in okPort:
                continue
            serverResult = serverScanObj((host, port))

            if isinstance(serverResult, str):
                scanResult.append((port, serverResult, None))
                okPort.append(port)

            elif isinstance(serverResult, tuple) and isinstance(serverResult[0], str):
                scanResult.append((port, serverResult[0], serverResult[1]))
                okPort.append(port)

    okResultList = []
    for port, serverInfo, serverHeader in scanResult:
        okResultList.append("%d => [%s]%s" % (port, serverInfo, "; Ver => %s" % serverHeader if serverHeader else ""))
        if serverInfo == "ssl":
            task_push("ssl", "https://%s/" % host)
        elif serverInfo == "www":
            if port != 80:
                pass
                # task_push("www", "http://%s:%d/" % (host, port))
        if serverInfo == "www":
            task_push("http", (host, port))
        else:
            task_push(serverInfo, (host, port))

    if okResultList:
        security_note("\n".join(okResultList))


if __name__ == '__main__':
    from dummy import *
    from dummy import _G

    _G["scanport"] = True
    audit(assign("ip", "127.0.0.1")[1])

    # KEY---bce1c8cb73e24b4a1518702a3080d00aad9583c2a25f4c59b4dffb06c009a25f---
